﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyClassServer.Net;

namespace EasyClassServer.Application.Email
{
    class Email : IApplication
    {
        public string Name => "email";

        private static int mailIndex = 0;

        public HttpResponseArgs Handle(HttpArgs args)
        {
            string address = (string)args.GetPostValue()["address"];
            string id = (string)args.GetPostValue()["id"];
            var smtp = "smtp.qq.com";
            var title = "EasyClass账号验证";
            var code = new Random().Next(200000, 999999);
            Data.Email email = new Data.Email
            {
                Host = smtp,
                MailSubject = title,
                MailFrom = "1137361788@qq.com",
                MailPwd = "rxjqbxyvxhrihggc",
                MailBody = "亲爱的" + id + ",欢迎使用EasyClass\n你的校验码是:" + code + ",五分钟内有效。",
                MailToArray = new string[] { address }
            };
            email.Send();
            var cachekey = "mail" + mailIndex.ToString();
            //存到缓存
            Data.CacheManager.SetCache(cachekey, code, 5);//五分钟内有效
            return new HttpResponseArgs()
            {
                Code = HttpCode.Sucsess,
                Message = "邮件已发送",
                Data = new EmailModel
                {
                    Code = code.ToString(),
                    Index = mailIndex++.ToString()
                }
            };
        }
    }
}
